package com.handinfo.android.core.graphics;

public class DWColor
{

	private int m_alpha, m_red, m_green, m_blue;

	public DWColor(int r, int g, int b, int alpha)
	{
		m_red = r;
		m_green = g;
		m_blue = b;
		m_alpha = alpha;
	}

	public DWColor(DWColor color)
	{
		this(color.m_red, color.m_green, color.m_blue, color.m_alpha);
	}

	public DWColor(int r, int g, int b)
	{
		this(r, g, b, 0xff);
	}

	public DWColor(float r, float g, float b)
	{
		this((int) (r * 255 + 0.5), (int) (g * 255 + 0.5),
				(int) (b * 255 + 0.5));

	}

	public DWColor(float r, float g, float b, float a)
	{
		this((int) (r * 255 + 0.5), (int) (g * 255 + 0.5),
				(int) (b * 255 + 0.5), (int) (a * 255 + 0.5));

	}

	public DWColor(int argb, boolean hasAlpha)
	{
		if (hasAlpha) {
			m_red = DWColor.getRed(argb);
			m_green = DWColor.getGreen(argb);
			m_blue = DWColor.getBlue(argb);
			m_alpha = DWColor.getAlpha(argb);
		}
		else {
			m_red = DWColor.getRed(argb);
			m_green = DWColor.getGreen(argb);
			m_blue = DWColor.getBlue(argb);
		}
	}

	public DWColor(int color)
	{
		m_red = getRed(color);
		m_green = getGreen(color);
		m_blue = getBlue(color);
		m_alpha = getAlpha(color);
	}

	public int getAlpha()
	{
		return m_alpha;
	}

	public int getRed()
	{
		return m_red;
	}

	public int getGreen()
	{
		return m_green;
	}

	public int getBlue()
	{
		return m_blue;
	}

	public static int getAlpha(int color)
	{
		return color >>> 24;
	}

	public void setAlpha(int alpha)
	{
		m_alpha = alpha;
	}

	public void setAlpha(float alpha)
	{
		setAlpha((int) (255 * alpha));
	}

	public static int getRed(int color)
	{
		return (color >> 16) & 0xff;
	}

	public static int getGreen(int color)
	{
		return (color >> 8) & 0xff;
	}

	public static int getBlue(int color)
	{
		return color & 0xff;
	}

	public int toIntValue(boolean hasAlpha)
	{
		int color = m_blue;
		color |= m_green << 8;
		color |= m_red << 16;
		if (hasAlpha) {
			color |= m_alpha << 24;
		}
		return color;
	}

	@Override
	public int hashCode()
	{
		int result = 1;
		result = 23 * result + m_alpha;
		result = 29 * result + m_blue;
		result = 31 * result + m_green;
		result = 37 * result + m_red;
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DWColor other = (DWColor) obj;
		if (m_alpha != other.m_alpha)
			return false;
		if (m_blue != other.m_blue)
			return false;
		if (m_green != other.m_green)
			return false;
		if (m_red != other.m_red)
			return false;
		return true;
	}

}
